package com.example.fz338.utils;

import com.github.leonardoxh.livedatacalladapter.LiveDataCallAdapterFactory;
import com.github.leonardoxh.livedatacalladapter.LiveDataResponseBodyConverterFactory;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetUtils {
    // 因为是 安卓虚拟机 所以 BASE_URL 是 10.0.2.2
    // https://developer.android.google.cn/studio/run/emulator-networking?hl=hu

    // https://blog.51cto.com/u_16213435/11681576
    // https://blog.51cto.com/u_16213398/11635187
    // https://www.bilibili.com/video/BV1yf4y1z7ho

    // public static final String BASE_URL = "http://10.0.2.2:8080/topline/"; // 虚拟机调试用
    public static final String BASE_URL = "http://192.168.43.172:8080/topline/"; // 真机调试用
    // 配置环境变量，给安卓虚拟机连WIFI使其能访问baidu，然后就解决了

    // 没开屏页: 说明没连上网 去检查能不能用手机浏览器上 www.baidu.com
    // 没主页数据: 说明没开 tomcat

    public static GetRequest get() {
        // 乱码问题 UTF-8 -> ANSI/GB2312
        // 乱码的原因是Android studio编辑器的编码设置问题 不是传输的数据乱码 已解决

        GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                // call->liveData (github)
                .addCallAdapterFactory(LiveDataCallAdapterFactory.create())
                .addConverterFactory(LiveDataResponseBodyConverterFactory.create())
                //string->json
                .addConverterFactory(gsonConverterFactory)
                .build();
        return retrofit.create(GetRequest.class);
    }
}
